import type { AuthApi } from './model/userModel'

import { baseRequestClient, requestClient } from '#/api/request'

/**
 * 短信验证码登陆
 * POST /login/smsLogin
 * 接口ID：216666820
 * 接口地址：https://app.apifox.com/link/project/5166966/apis/api-216666820
 */
export async function fetchLoginBySms(data: AuthApi.LoginCodeParams) {
    return await requestClient.post<AuthApi.LoginResult>('/login/smsLogin', data)
}

/**
 * 登录接口
 */
export async function fetchLogin(params: AuthApi.LoginCodeParams | AuthApi.LoginParams) {
    if ('mobile' in params) {
        return await fetchLoginBySms(params as AuthApi.LoginCodeParams)
    }
    const res = await requestClient.post<AuthApi.LoginResult>('/login/login', params)

    return res
}

/**
 * 图形验证码
 */
export async function fetchCaptcha() {
    return await requestClient.get<AuthApi.CaptchaResultModel>('/login/captcha')
}

/**
 * 获取登陆手机验证码
 * POST /login/smsCode
 * 接口ID：216652197
 * 接口地址：https://app.apifox.com/link/project/5166966/apis/api-216652197
 */
export async function fetchSendSms(data: { mobile: number | string }) {
    return await requestClient.post<{
        captcha: string
        code: number | string
    }>('/login/smsCode', data)
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
    return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
        withCredentials: true
    })
}
